package com.winai.launcher.utils;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.text.TextUtils;
import android.widget.Toast;

import com.ks.projectbasictools.utils.LogUtils;
import com.winai.launcher.manager.AiyishengManager;

import java.util.List;

@SuppressLint("WrongConstant")
public class OpenAppUtils {

    /**
     * @param routerPath 如:/forum/notice   调养须知
     */
    public static void startIntentPath(Context mContext, String routerPath, String remark) {
//        LogUtils.d("行为统计，进入" + remark + "页面");
//        AppCollectUtil.getInstance().activityDataCollect(remark, null, null, true, mContext, 1);
        try {
            AiyishengManager.getInstance(mContext).startAiyishengAct(routerPath);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(mContext, "您还没有安装相关APP", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 启动第三方apk
     * 如果已经启动apk，则直接将apk从后台调到前台运行（类似home键之后再点击apk图标启动），如果未启动apk，则重新启动
     *
     * @param packageName com.ks.demo
     */
    public static void launchAPK3(Context mContext, String packageName) {
        Intent intent = getAppOpenIntentByPackageName(mContext, packageName, "");
        try {
            mContext.startActivity(intent);
            /*((BaseActivity) mContext).overridePendingTransition(0, 0);*/
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(mContext, "您还没有安装相关APP", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * @param packageName  com.ks.demo
     */
    public static void launchAPK3(Context mContext, String packageName, String appName) {
        LogUtils.d("Launcher调用第三方应用：" + appName);
        Intent intent = getAppOpenIntentByPackageName(mContext, packageName, "");
        try {
            mContext.startActivity(intent);
            /*((BaseActivity) mContext).overridePendingTransition(0, 0);*/
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(mContext, "您还没有安装" + appName, Toast.LENGTH_SHORT).show();
        }
    }

    public static Intent getAppOpenIntentByPackageName(Context context, String packageName, String activityName) {
        String mainAct = null;
        PackageManager pkgMag = context.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
//        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);// FLAG_ACTIVITY_NEW_TASK、FLAG_ACTIVITY_RESET_TASK_IF_NEEDED

        if (TextUtils.isEmpty(activityName)) {
            List<ResolveInfo> list = pkgMag.queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);
            for (int i = 0; i < list.size(); i++) {
                ResolveInfo info = list.get(i);
                if (info.activityInfo.packageName.equals(packageName)) {
                    mainAct = info.activityInfo.name;
                    break;
                }
            }
        } else {
            mainAct = activityName;
        }
        if (TextUtils.isEmpty(mainAct)) {
            return null;
        }
        intent.setComponent(new ComponentName(packageName, mainAct));
        return intent;
    }
}
